package com.app;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import com.dao.DBManager;

/**
 * GiftApp
 */
public class GiftApp {

	private List<String> nameList = new ArrayList<String>();
	private List<String> fullNameList = new ArrayList<String>();
	private List<String> registeredUserList = new ArrayList<String>();
	
	private String checkForName;
	
	private DBManager dbManager;
	
	private CrypticService cyptServ;

	public GiftApp(){
		dbManager = new DBManager();
		cyptServ = new CrypticService();
		init();
	}
	
	public void init() {
		loadFullNameList();
		loadNameList();
		loadRegisteredUserList();
	}
	
	private void loadFullNameList() {
		fullNameList = dbManager.getFullNameList();
	}
	
	private void loadNameList() {
		nameList = dbManager.getNameList();
	}
	
	private void loadRegisteredUserList() {
		registeredUserList = dbManager.getRegisteredList();
	}
	
	public List<String> getFullNameList() {
		return fullNameList;
	}
	
	public boolean isNameExists() {
		return fullNameList.contains(checkForName);
	}
	
	public Integer getCubicleNumberFor(String user) {
		return dbManager.getCubicleNumberFor(user);
	}
	
	public String getAdmirer(String gifter){
		return cyptServ.decrypt(dbManager.getAdmirerName(gifter));
	}
	
	public String getRandomName() {
		Random randomNum = new Random();
		return getNameForIndex(randomNum.nextInt(nameList.size()));
	}
	public String getRandomNameFor(String gifter) {
		Random randomNum = new Random();
		String giftee = null;
		do{
		giftee = getNameForIndex(randomNum.nextInt(nameList.size()));
		}while(gifter.equals(giftee));
		return giftee;
	}
	public String getNameForIndex(Integer index) {
		return (String) nameList.get(index);
	}
	
	public void updateNameList(String name) {
		dbManager.updateNameList(name);
		loadNameList();
	}
	
	public void markUserAsRegistered(String user, String giftTo) {
		dbManager.updateRegisteredUser(user, cyptServ.encrypt(giftTo));
		loadRegisteredUserList();
	}
	
	public void setCheckForName(String name) {
		checkForName = name;
	}
	
	public String getCheckForName() {
		return checkForName;
	}
	
	public boolean isAlreadyRegistered(String user) {
		return registeredUserList.contains(user);
	}

	public boolean checkForSerialNumber(String slNo){
		if (getCheckForName().compareToIgnoreCase(dbManager.getNameForSerial(slNo)) == 0)
			return true;
		return false;
	}
}
